package com.framework.sog.advice;

import java.lang.reflect.Method;

import org.apache.commons.lang.StringUtils;

import com.framework.bas.advice.AbsThrowAdvice;
import com.framework.bas.bs.IPlmBasBS;
import com.framework.bas.param.GeneralParam;
import com.framework.bas.service.IControl;
import com.framework.bas.util.PlmBasConstant;
import com.framework.bas.util.UtilMessage;
import com.framework.bas.vo.BasService;
import com.framework.sog.bs.IPlmSogBS;
import com.framework.sog.util.PlmSogConstant;

public class ServiceThrowLog extends AbsThrowAdvice {
	
	private IPlmSogBS plmSogBS;
	private IPlmBasBS plmBasBS;

	public IPlmSogBS getPlmSogBS() {
		return plmSogBS;
	}

	public void setPlmSogBS(IPlmSogBS plmSogBS) {
		this.plmSogBS = plmSogBS;
	}

	public IPlmBasBS getPlmBasBS() {
		return plmBasBS;
	}

	public void setPlmBasBS(IPlmBasBS plmBasBS) {
		this.plmBasBS = plmBasBS;
	}

	public void throwHandle(Method method, Object[] args, Object target,
			Exception ex) {
		boolean paramFlag = false;
		int pos = 0;
		if ((args!=null) && (target instanceof IControl)){
			for(int i=0 ; i<args.length; i++){
				if(args[i] instanceof GeneralParam){
					paramFlag = true;
					pos = i;
					break;
				}
			}
			if(paramFlag){
				GeneralParam param = (GeneralParam) args[pos];
				String serviceCode = param.getAppService();
				if(param.getValueObject(PlmBasConstant.APP_SERVICE_OBJECT) == null){
					//封装交易信息
					BasService hpBasServiceVO = getPlmBasBS().queryServiceByCode(serviceCode);
					param.setValue(PlmBasConstant.APP_SERVICE_OBJECT, hpBasServiceVO);
				}
				//判断该交易是否记录日志
				String serviceName="";
				Object[] objs = new Object[] {serviceName};
				String info = null;
				if(StringUtils.isBlank(ex.getMessage())){
					Throwable caseObj = ex.getCause();
					while(caseObj!=null){
						if(StringUtils.isBlank(caseObj.getMessage())){
							caseObj = caseObj.getCause();
						}else{
							break;
						}
					}
					if(caseObj!=null){
						objs = new Object[] {serviceName, caseObj.getMessage()};
						info = UtilMessage.getMessage(PlmSogConstant.MSG_0002, objs, null, caseObj.getClass());
					}else{
						objs = new Object[] {serviceName, ex.getMessage()};
						info = UtilMessage.getMessage(PlmSogConstant.MSG_0002, objs);
					}
				}else{
					objs = new Object[] {serviceName, ex.getMessage()};
					info = UtilMessage.getMessage(PlmSogConstant.MSG_0002, objs);
				}
				//获取交易日志
			}
		}

	}

}
